package action;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import modeles.Contact;
import controller.ControllerTab;
import utilitaire.MyFile;
import vues.HomeWindow;
import vues.NewContactWindow;

/**
 * 
 * Class Action Sauvegarde de contact
 * newContact	Objet NewContactWindow
 * homeView		Objet HomeWindow
 * 
 * @author Paul BEZARD-FALGAS
 *
 */
public class ActionSaveContact extends AbstractAction {

	private static final long serialVersionUID = 1L;
	private NewContactWindow newContact;
	private HomeWindow homeView;

	public ActionSaveContact(HomeWindow home, NewContactWindow newContactWindow){
		super();
		this.homeView = home;
		this.newContact = newContactWindow;
	}	  


	public void actionPerformed(ActionEvent e){
		SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				JOptionPane jop1;
				boolean save=false;
				String infoContact = null;
				Contact contact = new Contact();
				
				contact.setName(newContact.getField1());
				contact.setFirstName(newContact.getField2());
				contact.setCompanyAddress(newContact.getField3());
				contact.setPersonalPhone(newContact.getField4());
				contact.setProfessionalPhone(newContact.getField5());
				contact.setHomePage(newContact.getField6());
				contact.setEmail(newContact.getField7());
				contact.setPicture(newContact.getField8());
				contact.setOther(newContact.getField9());
				contact.setGroup(newContact.getlistGrp());
				
				//Mise en forme des informations du contact sous forme de string
				infoContact = contact.getStringInfo();
				
				//Ecriture du contact dans le fichier txt
				MyFile file = new MyFile();
				save = file.writeTextFile(infoContact);
				
				//Test si la sauvegarde a r�ussi
				if(save)
				{
					//Ouverture fenetre du menu
					ActionMain actionMain = new ActionMain(homeView);
					
					//Boite du message d'information
					jop1 = new JOptionPane();
					jop1.showMessageDialog(null, "Save Success", "Information", JOptionPane.INFORMATION_MESSAGE); 
				}else{
					//Boite du message d'erreur
					jop1 = new JOptionPane();
					jop1.showMessageDialog(null, "Error with contact", "Erreur", JOptionPane.ERROR_MESSAGE);
				}

			}
		});
	}

}
